// pages/biz/browsingHistory/browsingHistory.js
const app = getApp();
import Serv from './queryHouseServ';
import Config from '../../../utils/config2.js'
import cardConfig from '../../../config.js'
var regeneratorRuntime = require("../../../lib/js/runtime-module.js")
var sensorsdata_conf = require("../../../utils/sensorsdata_conf.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    accountId: '',
    params: {},
    listData: [],
    // pageNo: 1,
    hasMore:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function(options) {
    let params ={...params,productIds:options.productId||''}
    this.setData({
      params,
      gameActivityId:options.gameActivityId||''
    })
  },
  //找房接口
  async getHouseList(parmas, isPush) {
    wx.showLoading({
      title: '加载中',
    })
    console.log('找房数据参数-->', parmas)
    let result = await Serv.getfindList(parmas)
    if (result.success) {
      let listData = result.rows;
      if (isPush == 0) {
        this.setData({
          listData: listData,
          notBeginData: result
        })
        console.log(this.data.listData)
      } else {
        let notBeginData = this.data.notBeginData // 获取原本保存的数据
        notBeginData.pageNo = result.pageNo // 新的当前页
        let rows = result.rows // 数据列表
        let listData = this.data.listData
        let oldrows = listData // 原本的数据列表
        let newList = oldrows.concat(rows) // 合并
        listData = newList // 新列表合并后赋值
        this.setData({
          listData: listData,
          notBeginData: notBeginData
        })
      }
      wx.hideLoading();
    }
  },
  toBuildingDetail: function (e) {
    let index = e.currentTarget.dataset.index;
    const that = this;
    sensorsdata_conf.server_url = `${Config.host}app/dataAnalysis/create`
    sensorsdata_conf.batch_send.send_timeout = 0
    app.globalData.sensors.setPara(sensorsdata_conf);
    app.globalData.sensors.track('gameEvent', {gameActivityId: this.data.gameActivityId,productId:this.data.listData[index].productId});
    wx.navigateTo({
      url: `/subPackages/pages/buildingDetail/buildingDetail?productId=${this.data.listData[index].productId}`,
      // success: function(){
      //   app.globalData.findHouseData = that.data.params;
      // }
    })
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    let {
      city = '',
      cityadcode = '',
    } = app.globalData.cityData || {};
    if (city.indexOf('市') > -1) {
      city = city.slice(0, -1);
    }
    
    const params = {
      ...this.data.params,
      city: cityadcode
    }
    this.getHouseList(params,0);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    sensorsdata_conf.server_url =`${cardConfig.config.pageDomain}applet/visitLog/create`
    sensorsdata_conf.batch_send.send_timeout = 6000
    app.globalData.sensors.setPara(sensorsdata_conf);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    sensorsdata_conf.server_url =`${cardConfig.config.pageDomain}applet/visitLog/create`
    sensorsdata_conf.batch_send.send_timeout = 6000
    app.globalData.sensors.setPara(sensorsdata_conf);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let total = Number.parseInt(this.data.notBeginData.total) // 总条数
    let totalPage = 1 // 总页数
    if (total % 10 === 0) {
      totalPage = Number.parseInt(total / 10)
    } else {
      totalPage = Number.parseInt(total / 10) + 1
    }
    let pageNo = Number.parseInt(this.data.notBeginData.pageNo) // 当前页数
    if (pageNo < totalPage && (pageNo + 1) <= totalPage) { // 如果当前条数小于总条数
      let pageParams = {
        ...this.data.params,
        city: app.globalData.cityData?app.globalData.cityData.cityadcode:null,
        pageNo: pageNo + 1,
        pageSize: 10,
        name:this.data.name
      }
      this.getHouseList(pageParams, 1)
    }
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function() {

  // }
})